const router = require("koa-router")();
const puppeteer = require("../utils/puppeteer");
const puppeteerX = require("../utils/puppeteerX");

router.get("/api/pdf/export", async (ctx, next) => {
  try {
    console.log(ctx.query.name);
    await puppeteer.eventHandler(ctx);
  } catch (error) {
    ctx.throw(500, error);
    next();
  }
});

/**
 * post 请求，获取html，并生成pdf文件
 * @param {Object} pageInfo 页面地址--封面，内容，尾页
 * @param {Object} pageTemplate 页眉页脚参数
 * @param {Object} pageStyle 页边距参数
 * @param {Object} pageCache 缓存参数
 * return fileStream
 */
router.post("/api/generatePDF", async (ctx, next) => {
  try {
    await puppeteerX.generatePDF(ctx);
  } catch (error) {
    ctx.throw(500, error);
    next();
  }
});

module.exports = router;
